package org.molehill
{
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.textures.Texture;
	import flash.geom.Matrix3D;

	public class SpriteMoleHill implements ISpriteMoleHill
	{
		private var _x:Number;
		private var _y:Number;
		private var _width:Number;
		private var _height:Number;
		private var _widthMolehill:int;
		private var _heightMolehill:int;
		private var _scaleX:Number;
		private var _scaleY:Number;
		private var _alpha:Number;
		private var _url:String;
		public var viewMatrix:Matrix3D;
		
		private var _context:Context3D;
		private var _matrix:Matrix3D;
		private var _bitmapdata:BitmapData;
		private var _texture:Texture;
		public var indexBuffer:IndexBuffer3D;
		
		public function SpriteMoleHill(context:Context3D)
		{
			_context    = context;
			_matrix     = new Matrix3D();
			_x = 0;
			_y = 0;
			_scaleX = 0;
			_scaleY = 0;
			_alpha = 1;
		}
		
		private function getWorldTransform():Matrix3D
		{
			_matrix.identity();
			_matrix.appendScale(_widthMolehill * 0.5, _heightMolehill * 0.5, 1);
			_matrix.appendTranslation((_widthMolehill / 2) + x, (_heightMolehill / 2) + y, 0);
			return _matrix;
		}
		
		public function render():void{
			var mvp:Matrix3D = this.getWorldTransform();
			mvp.append(viewMatrix);
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, mvp, true);
			_context.setTextureAt(1, _texture);
			_context.drawTriangles(indexBuffer, 0, 2);
		}
		
		public function refreshBitmap(value:BitmapData):void
		{
			_bitmapdata = value;
			_texture.uploadFromBitmapData(_bitmapdata);
		}
		
		public function set alpha(value:Number):void
		{
			this._alpha = value;
		}
		
		public function get alpha():Number
		{
			return this._alpha;
		}
		
		public function get height():Number
		{
			return this._height;
		}
		
		public function get width():Number
		{
			return this._width;
		}
		
		public function set x(value:Number):void
		{
			this._x = value;
		}
		
		public function get x():Number
		{
			return this._x;
		}
		
		public function set y(value:Number):void
		{
			this._y = value;
		}
		
		public function get y():Number
		{
			return this._y;
		}

		public function get bitmapdata():BitmapData
		{
			return _bitmapdata;
		}

		public function set bitmapdata(value:BitmapData):void
		{
			_bitmapdata = value;
			this._widthMolehill = MoleHillUtils.getTextureWH(_bitmapdata.width);
			this._heightMolehill = MoleHillUtils.getTextureWH(_bitmapdata.height);
			this._width = _bitmapdata.width;
			this._height = _bitmapdata.height;
			this._texture = _context.createTexture(_widthMolehill,_heightMolehill, Context3DTextureFormat.BGRA, false);
			_texture.uploadFromBitmapData(_bitmapdata);
			//TexturePool.getInstance().addTexture(_url,_texture); 
		}

		public function set url(value:String):void
		{
			_url = value;
		}


	}
}